﻿using MediatR;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using SlaughterItem.Common.Domains.UPCTable;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace SlaughterItem.API.Application.Commands.Week02.UPC
{
    //显示商品代号表数据
    public class ShowUPCDataHandler : IRequestHandler<ShowUPCDataCommand, Result>
    {
        public ShowUPCDataHandler(IBaseRepository<UPCData> uPCDataRepository, IBaseRepository<UPCTypeData> uPCTypeDataRepository)
        {
            UPCDataRepository = uPCDataRepository;
            UPCTypeDataRepository = uPCTypeDataRepository;
        }

        public IBaseRepository<UPCData> UPCDataRepository { get; set; }
        public IBaseRepository<UPCTypeData> UPCTypeDataRepository { get; set; }
        public async Task<Result> Handle(ShowUPCDataCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };
            var list = (from a in UPCDataRepository.GetAll()
                        join b in UPCTypeDataRepository.GetAll()
                        on a.CommodityId equals b.CommodityId
                        select new ShowUPCDataDTO
                        {
                            GoodsMinuteId =a.GoodsMinuteId,
                            MemoryNum = a.MemoryNum,
                            CommodityId =a.CommodityId,
                            OrderNum =a.OrderNum,
                            GoodsGradeId =a.GoodsGradeId,
                            UPCDataName =a.UPCDataName,
                            IsGoodsMinute =a.IsGoodsMinute,
                            CommodityName =b.CommodityName
                        }).ToList();
            if (list != null)
            {
                result.Data = list;
                result.Code = (int)CommonErrorCode.Success;
                return result;
            }
            else
            {
                return result;
            }
        }
    }
}
